//
//  LPZWYSearchViewController.m
//  ZhongWangYuanApp
//
//  Created by liuyingping on 2018/6/4.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LPZWYSearchViewController.h"
#import "LPZWYChooseClassClassModel.h"
#import "ZJVidoViewController.h"
#import "LPZWYCCTableHeaderView.h"
#import "LPZWYCommonClassTableViewCell.h"
#define classCell @"LPZWYCommonClassTableViewCell"
@interface LPZWYSearchViewController ()<UITableViewDelegate,UITableViewDataSource>
/**    */
@property (strong, nonatomic) UITableView  *tableView;
@property (nonatomic , strong) NSMutableArray * dataArray;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  页数  */
@property (assign, nonatomic) NSInteger  page;


@end

@implementation LPZWYSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"最新课程";
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,LP_NavViewHeight, [UIScreen zj_screenWith], 50 * [UIScreen zj_screenHScale])];
//    label.text = @"    最新课程";
//    label.textColor = [UIColor textGrayColor];
//    label.font = LYPFONT(16);
//    [self.view addSubview:label];
    
    
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self.tableView.mj_header beginRefreshing];
    [self layoutUI];
}



#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    [LPRequestManager request_getcoursewarelistforwcateWithcondation:self.searchString pageindex:[NSString stringWithFormat:@"%ld",self.page] threecate:self.threeId twocate:self.secondId onecate:self.firstId year:self.timeString successBlock:^(id object) {
        NSString *result = [[object objectForKey:@"data"] objectForKey:@"result"];
        if ([result integerValue] == 1) {
            NSArray *array = [[object objectForKey:@"data"] objectForKey:@"list"];
            if (array.count == 0) {
                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                
            }else{
                weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array] mutableCopy];
                [weakSelf.tableView reloadData];
            }
            
        }else{
            [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
        }
        [weakSelf.tableView.mj_header endRefreshing];
        
    } failBlock:^(id object) {
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
    
}

//上拉加载
- (void)netWorking_footer{
    
    __weak typeof(self) weakSelf = self;
    [LPRequestManager request_getcoursewarelistforwcateWithcondation:self.searchString pageindex:[NSString stringWithFormat:@"%ld",self.page] threecate:self.threeId twocate:self.secondId onecate:self.firstId year:self.timeString successBlock:^(id object) {
        NSString *result = [[object objectForKey:@"data"] objectForKey:@"result"];
        if ([result integerValue] == 1) {
            NSArray *array = [[object objectForKey:@"data"] objectForKey:@"list"];
            if (array.count == 0) {
                _page --;
            }else{
                [weakSelf.dataArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array]] ;
                [weakSelf.tableView reloadData];
            }
            
        }
        
        [weakSelf.tableView.mj_footer endRefreshing];
    } failBlock:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
    
}

#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.navigView , 0)
    .bottomSpaceToView(self.view , 0);
    
    
}


#pragma mark - 私有方法


#pragma mark - 监听事件



#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 125 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LPZWYCommonClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:classCell forIndexPath:indexPath];
    cell.isMintue = YES;
    cell.model = self.dataArray[indexPath.row];
    return cell;
    
    
}


/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (YBUSERINFOMODEL.isCloudyAutonomouslyChooseStudy) {
        ZJVidoViewController *vc = [ZJVidoViewController new];
        vc.model = self.dataArray[indexPath.row];
        [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
    }else{
        [YJProgressHUD showSuccess:@"您没有自主选学权限!" inview:[UIApplication sharedApplication].keyWindow];
    }
    
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_NavViewHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_NavViewHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
        [_tableView registerClass:[LPZWYCommonClassTableViewCell class] forCellReuseIdentifier:classCell];
         [_tableView adaptiveIOS11];
        
    }
    return _tableView;
}


- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

@end
